function map(arr, callback) {
	let array = [];
	for (let i = 0; i < arr.length; i++) {
		array.push(callback(arr[i], i));
	}
	return array;
}

function reduce(arr, callback, init) {
	let result = init;
	for (let i = 0; i < arr.length; i++) {
		result = callback(result, arr[i]);
	}
	return result;
}


function filter(arr, callback) {
	let result = [];
	for (let i = 0; i < arr.length; i++) {
		if (callback(arr[i])) {
			result.push(arr[i])
		}
	}
	return result;
}

function find(arr, callback) {
	for (let i = 0; i < arr.length; i++) {
		if (callback(arr[i])) {
			return arr[i]
		}
	}
	return undefined;
}


function findIndex(arr, callback) {
	for (let i = 0; i < arr.length; i++) {
		if (callback(arr[i])) {
			return i;
		}
	}
	return -1;
}

// 每一个都为真
function every(arr, callback) {
	for (let i = 0; i < arr.length; i++) {
		if (!callback(arr[i], i)) {
			return false;
		}
	}
	return true;
}

// 有一个为真 就为真
function some(arr, callback) {
	for (let i = 0; i < arr.length; i++) {
		if (callback(arr[i], i)) {
			return true;
		}
	}
	return false;
}

function unique1(arr) {
	let array = [];
	for (let i = 0; i < arr.length; i++) {
		if (!array.includes(arr[i])) {
			array.push(arr[i]);
		}
	}
	return array;
}

function unique2(arr) {
	let obj = {}, result = [];
	for (let i = 0; i < arr.length; i++) {
		if (!obj.hasOwnProperty(arr[i])) {
			obj[arr[i]] = true;
			result.push(arr[i])
		}

	}
	return result;
}

function unique(arr) {
	let set = new Set(arr);
	return [...set];
}


function concat(arr, ...args) {
	let array = [];
	[...arr, ...args].forEach(item => {
		if (Array.isArray(item)) {
			array.push(...item)
		} else {
			array.push(item)
		}
	})
	return array;
}

function slice(arr, start, end) {
	if (arr.length === 0) return [];
	start = start || 0;
	if (start > arr.length) return [];
	end = end || arr.length;
	let result = [];
	for (let i = 0; i < arr.length; i++) {
		if (i >= start && i < end) {
			result.push(arr[i]);
		}
	}
	return result;
}


function flatten1(arr) {
	let result = []
	arr.forEach(item => {
		if (Array.isArray(item)) {
			result = result.concat(flatten1(item))
		} else {
			result = result.concat(item)
		}
	})
	return result;
}

function flatten2(arr) {
	let result = [...arr];
	while (result.some(item => Array.isArray(item))) {
		result = [].concat(...result);
	}
	return result;
}


function chunk(arr, size = 1) {
	if (arr.length === 0) return [];
	let result = [], temp = [];
	arr.forEach(item => {
		if (temp.length === 0) {
			result.push(temp);
		}
		temp.push(item);
		if (temp.length === size) {
			temp = [];
		}
	})
	return result;
}

/**
 * 数组差集
 * @param arr1
 * @param arr2
 * @return {*}
 */
function array_diff(arr1, arr2) {
	return arr1.filter(item => !arr2.includes(item));
}

/**
 * 删除数组中某些元素,返回删除的元素
 * @param arr
 * @param args
 * @return {*[]}
 */
function pull(arr, ...args) {
	let result = [];
	for (let i = 0; i < arr.length; i++) {
		if (args.includes(arr[i])) {
			result.push(arr[i]);
			arr.splice(i, 1);
			i--;
		}
	}
	return result;
}

/**
 * 获取数组部分
 * @param {[]} arr
 * @param {Number} size
 * @return {[]}
 */
function drop(arr, size) {
	return arr.filter((item, index) => index >= size);
}
